package com.source.barcodeMachineStatus.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.source.barcodeMachineStatus.entity.BarcodeMachineStatus;
import com.source.barcodeMachineStatus.service.BarcodeMachineStatusService;
import com.source.barcodeMachineStatus.vo.*;
import com.source.utils.CommonResult;
import com.source.utils.MyPage;
import com.source.utils.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author fengq
 * @since 2021-02-27
 */
@Api(tags = "脚标机监控管理")
@RestController
@RequestMapping("barcodeMachineStatus/barcodeMachineStatus")
@CrossOrigin
public class BarcodeMachineStatusController {
    @Autowired
    private BarcodeMachineStatusService barcodeMachineStatusService;


//    @ApiOperation(value = "保存一个监控信息信息")
//    @PostMapping("/save")
//    public CommonResult<BarcodeMachineStatus> save(@RequestBody BarcodeMachineStatus barcodeMachineStatus){
//        boolean bool = barcodeMachineStatusService.save(barcodeMachineStatus);
//        if (bool){
//            return new CommonResult<BarcodeMachineStatus>(ResultEnum.SUCCESS.getCode(),ResultEnum.SUCCESS.getMsg(),barcodeMachineStatus);
//        }else {
//            return new CommonResult<BarcodeMachineStatus>(ResultEnum.ERROR.getCode(),ResultEnum.ERROR.getMsg(),null);
//        }
//    }


    @ApiOperation(value = "信息展示分页查询，pageCurrent当前页数，pageSize每一页的记录数，默认值为1/10")
    @GetMapping("/findPageVo")
    public CommonResult<IPage<BarcodeMachineStatusVo>> findPageVo(@RequestParam(value = "pageCurrent", defaultValue = "1") Integer pageCurrent,
                                                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<BarcodeMachineStatusVo> page = new Page<>(pageCurrent, pageSize);
        IPage<BarcodeMachineStatusVo> pageVo = barcodeMachineStatusService.getPageVo(page);

        return new CommonResult<IPage<BarcodeMachineStatusVo>>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), pageVo);

    }

    @ApiOperation(value = "模糊查询，根据角标机编号，或登录时间，或在线状态，或条形码剩余数量，或用户，或下线时间")
    @GetMapping("/findByLike")
    public CommonResult<IPage<BarcodeMachineStatusVo>> findByLike(@RequestParam("keyword") String keyword,
                                                                  @RequestParam(value = "pageCurrent", defaultValue = "1") Integer pageCurrent,
                                                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<BarcodeMachineStatusVo> page = new Page<>(pageCurrent, pageSize);
        IPage<BarcodeMachineStatusVo> voIPage = barcodeMachineStatusService.getLikePageVo(page, keyword);

        return new CommonResult<IPage<BarcodeMachineStatusVo>>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), voIPage);

    }

    //这个先保留
    @ApiOperation(value = "根据status主键id进行单个查询,查询返回展示信息类，分页参数默认为1/10",
            notes = "id是信息状态表的id(建议测试id为9),不是barcodeMachineId,BarcodeMachineStatusVo信息展示类中，还有分页档口信息展示类StallsMessageVo")
    @GetMapping("/findOneVoById/{id}")
    public CommonResult<BarcodeMachineStatusVo> findOneVoById(@PathVariable("id") String id,
                                                              @RequestParam(value = "pageCurrent", defaultValue = "1") Integer pageCurrent,
                                                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        BarcodeMachineStatusVo barcodeMachineStatusVo = null;
        try {
            barcodeMachineStatusVo = barcodeMachineStatusService.getOneVo(id);
            //List<StallsMessageVo> list = barcodeMachineStatusService.getStallsMessageVoById(barcodeMachineStatusVo.getBarcodeMachineId());
            List<StallsMessageVo> list = barcodeMachineStatusService.getStallsMessageVoById(barcodeMachineStatusVo.getBarcodeMachineNumber());

            Page<StallsMessageVo> page = MyPage.list2Page(pageCurrent, pageSize, list);
            barcodeMachineStatusVo.setStallsMessageVoPage(page);
        } catch (Exception e) {
            return new CommonResult<BarcodeMachineStatusVo>(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(), barcodeMachineStatusVo);
        }
        return new CommonResult<BarcodeMachineStatusVo>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), barcodeMachineStatusVo);
    }

    @ApiOperation(value = "更新/修改脚标机的状态", notes = "id是信息状态表的id,不是barcodeMachineId")
    @PutMapping("/updateById")
    public CommonResult<BarcodeMachineStatus> updateById(@RequestBody BarcodeMachineStatus barcodeMachineStatus) {
        try {
            boolean bool = barcodeMachineStatusService.updateById(barcodeMachineStatus);
        } catch (Exception e) {
            e.printStackTrace();
            return new CommonResult<BarcodeMachineStatus>(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(), barcodeMachineStatus);
        }
        return new CommonResult<BarcodeMachineStatus>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), barcodeMachineStatus);
    }

    @ApiOperation(value = "根据id上删除一条记录")
    @DeleteMapping("/deleteById/{id}")
    public CommonResult<Boolean> deleteById(@PathVariable("id") String id) {
        boolean bool = barcodeMachineStatusService.removeById(id);
        if (bool) {
            return new CommonResult<Boolean>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), true);
        } else {
            return new CommonResult<Boolean>(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(), false);
        }
    }

    @ApiOperation(value = "批量删除记录")
    @DeleteMapping("/deleteByIds")
    public CommonResult<Boolean> deleteByIds(@RequestParam("ids") List<String> ids) {
        boolean bool = barcodeMachineStatusService.removeByIds(ids);
        if (bool) {
            return new CommonResult<Boolean>(ResultEnum.SUCCESS.getCode(), ResultEnum.SUCCESS.getMsg(), true);
        } else {
            return new CommonResult<Boolean>(ResultEnum.ERROR.getCode(), ResultEnum.ERROR.getMsg(), false);
        }
    }

    @ApiOperation(value = "查询脚标机的所有状态的数量")
    @GetMapping("/Count/{houseId}")
    public CommonResult getCountBarcodeByStatus(@PathVariable("houseId") String houseId){
        List<BarcodeMachineStatusCountVo> list = barcodeMachineStatusService.selectCountBarcodeMachineByStatus(houseId);
        if (list.isEmpty()){
            return CommonResult.error("查询结果为空");
        }else {
            return  CommonResult.error("查询成功").data(list);
        }
    }

    @ApiOperation(value = "根据屠宰场id查询分布情况")
    @GetMapping("/address/{houseId}")
    public CommonResult getBarcodeAddressByHouseId(@PathVariable("houseId") @ApiParam("屠宰场id") String houseId,
                                                   @RequestParam(value = "state",required = false) @ApiParam(value = "在线状态(0:离线，1:正常，2：卡纸，3：缺纸)",required = false) String state){
        List<BarcodeMachineAddressVo> list = barcodeMachineStatusService.selectBarcodeMachineAddressByHouseId(houseId,state);
        if (list.isEmpty()){
            return CommonResult.error("查询结果为空");
        }else {
            return CommonResult.success("查询成功").data(list);
        }
    }

    @ApiOperation(value = "根据市场id和脚标机状态查询档口名、条形码剩余量")
    @GetMapping("/statusChart/{current}/{size}/{houseId}")
    public CommonResult getBarcodeCountAndStallNameAndStatusChart(@PathVariable("current") @ApiParam("当前页") int current,
                                                                  @PathVariable("size") @ApiParam("每页记录数") int size,
                                                                  @PathVariable("houseId") @ApiParam("屠宰场id") String houseId,
                                                   @RequestParam(value = "state",required = false) @ApiParam(value = "在线状态(0:离线，1:正常，2：卡纸，3：缺纸)",required = false) String state){
        Page<BarcodeCountChart> page=new Page<>(current,size);
        IPage<BarcodeCountChart> chartIPage = barcodeMachineStatusService.getBarcodeCountAndStallNameAndStatus(page, houseId, state);
        if (chartIPage.getRecords().isEmpty()||chartIPage.getTotal()==0){
            return CommonResult.error("查询结果为空");
        }else {
            return CommonResult.success("查询成功").data(chartIPage);
        }
    }

    @ApiOperation(value = "根据市场id和关键词查询脚标机位置信息")
    @GetMapping("/address/{houseId}/{keyword}")
    public CommonResult getBarcodeAddressByHouseIdAndKeyword(@PathVariable("houseId") @ApiParam("屠宰场id") String houseId,
                                                             @PathVariable("keyword") @ApiParam(value = "市场名称，档口名称、脚标机编号") String keyword){
        List<BarcodeMachineAddressVo> list = barcodeMachineStatusService.getBarcodeMachineAddressByHouseIdAndKeyword(houseId, keyword);
        if (list.isEmpty()){
            return CommonResult.error("查询结果为空");
        }else {
            return CommonResult.success("查询成功").data(list);
        }
    }

}

